package com.ll.daipao.controller;

import com.alibaba.fastjson.JSONObject;
import com.ll.daipao.handler.Constants;
import com.ll.daipao.service.OrderService;
import com.ll.daipao.util.Sha1Util;
import com.ll.daipao.vo.TOrder;
import com.ll.daipao.vo.TOrderType;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by rockyll on 2017/10/26.
 */
@Controller
@RequestMapping("send")
public class SendOrderController extends BaseController
{
    @Autowired
    OrderService orderService;

    @Resource(name = "wxMpService2")
    protected WxMpService wxMpService2;

    @RequestMapping("taskObtain")
    public String taskObtain (HttpServletRequest request){
        List<TOrderType> tOrderTypes = orderService.selectOrderType();
        request.setAttribute("ordertypes" ,tOrderTypes );
        try {
            String jsapiTicket = wxMpService2.getJsapiTicket();
            String appid = wxMpService2.getWxMpConfigStorage().getAppId();
            String timestamp = Sha1Util.getTimeStamp();
            String nonceStr = Sha1Util.getNonceStr();
            SortedMap<String,String> sigmap = new TreeMap<>();
            sigmap.put("noncestr",nonceStr);
            sigmap.put("jsapi_ticket",jsapiTicket);
            sigmap.put("timestamp",timestamp);
            sigmap.put("url",Constants.REAL_URL+"daipao/send/taskObtain");
            try {
                String signature = Sha1Util.createSHA1Sign(sigmap);
                request.setAttribute("signature",signature);
            } catch (Exception e) {
                e.printStackTrace();
            }

            request.setAttribute("timestamp",timestamp);
            request.setAttribute("appId",appid);
            request.setAttribute("nonceStr",nonceStr);

        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return "taskObtain";
    }

    /**
     * 发单
     * @param request
     * @return
     */
    @RequestMapping(value = "saveOrder",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String saveOrder(HttpServletRequest request ){

        String openId = (String) request.getSession().getAttribute(Constants.WX_OPENID);

        String order_title = request.getParameter("order_title");
        String order_desc = request.getParameter("order_desc");
        String re_address = request.getParameter("re_address");
        String tel = request.getParameter("tel");
        String amount = request.getParameter("amount");
        String order_type = request.getParameter("order_class");
        String btime = request.getParameter("btime");
        String etime = request.getParameter("etime");
        if(StringUtils.isBlank(order_title)
                ||StringUtils.isBlank(order_desc)
                ||StringUtils.isBlank(re_address)
                ||StringUtils.isBlank(tel)
                ||StringUtils.isBlank(amount)
                ||StringUtils.isBlank(order_type)
                ||StringUtils.isBlank(btime)
                ||StringUtils.isBlank(etime)){
            return  failed("关键信息不得为空！");
        }
       TOrder tOrder = new TOrder();
        tOrder.setAmount(Long.parseLong(amount));
        tOrder.setOrderDesc(order_desc);
        tOrder.setOrderTitle(order_title);
        tOrder.setReAddress(re_address);
        tOrder.setTel(tel);
        tOrder.setCreateTime(new Date());
        tOrder.setOrderType(order_type);
        tOrder.setReBeginTime(new Date());
        tOrder.setReEndTime(new Date());
        JSONObject res = null;
        try {
            res = orderService.saveOrder(openId, tOrder);
        } catch (Exception e) {
            this.logger.error(e.getMessage(),e);
            return failed();
        }
        if(res.getBoolean("suc")){
            res.remove("suc");
            return success(res);
        }
        return failed(res.getString("msg"));
    }

}
